package org.wheres;

import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.XMPPException;
import org.wheres.core.Communicator;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class NewUser extends Activity {

	private EditText user;
	private EditText pass;
	private EditText mail;
	private EditText trueName;
	private CheckBox checkBox;
	
	private Map<String,String> mappa;
	private Communicator communicatorService;
	private boolean boundToService = false;
	
	private Intent serviceIntent;
	
	private ServiceConnection communicatorConnection = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			communicatorService = ((Communicator.CommunicatorBinder)service).getService();
			boundToService = true;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			communicatorService = null;
			boundToService = false;
		}	
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_user);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		serviceIntent = new Intent(NewUser.this, Communicator.class);
		bindService(serviceIntent, communicatorConnection, Context.BIND_AUTO_CREATE);
	}

	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (this.boundToService)
			unbindService(communicatorConnection);
	}
	
	public void onClickSubmit(View view){
		
		user = (EditText) findViewById(R.id.regUser);
		pass = (EditText) findViewById(R.id.regPass);
		mail = (EditText) findViewById(R.id.regMail);
		trueName = (EditText) findViewById(R.id.regName);
		checkBox = (CheckBox) findViewById(R.id.checkLog);
		
		mappa = new HashMap<String,String>();
		mappa.put("email", mail.getText().toString());
		mappa.put("name", trueName.getText().toString());
		
		
		if(user.getText().toString().equals("") || pass.getText().toString().equals(""))
			Toast.makeText(this, R.string.UserOrPassEmpty, Toast.LENGTH_LONG).show();
		else{
			try {
				this.communicatorService.createAccount(user.getText().toString(), pass.getText().toString(), mappa);
				if(checkBox.isChecked())
					this.communicatorService.startConnection(user.getText().toString(), pass.getText().toString());
				else{
					user.setText("");
					pass.setText("");
					mail.setText("");
					trueName.setText("");
				}
			} catch (XMPPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		}

}
